package com.lanou.beautifulwardrobe.Home;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.android.volley.AuthFailureError;
import com.android.volley.NetworkResponse;
import com.android.volley.ParseError;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.HttpHeaderParser;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.Gson;
import com.lanou.beautifulwardrobe.R;
import com.lanou.beautifulwardrobe.Home.Adapter.HomeViewPagerAdapter;
import com.lanou.beautifulwardrobe.contants.Contants;
import com.lanou.beautifulwardrobe.contants.VolleySingleton;
import com.lanou.beautifulwardrobe.entify.TagItem;
import com.lanou.beautifulwardrobe.entify.TagItemResult;

import android.support.design.widget.TabLayout;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * Created by dllo on 15/10/21.
 */
public class HomeFragment extends Fragment {
    private static HomeFragment homeFragment;
    private ViewPager viewPager;
    private Context context;
    private HomeViewPagerAdapter adapter;
    private TabLayout tabLayout;


    public static HomeFragment getHomeFragment() {
        homeFragment = new HomeFragment();
        return homeFragment;
    }


    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.context = context;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.mainfragment_home, null);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        tabLayout = (TabLayout) view.findViewById(R.id.tabLayout);
        viewPager = (ViewPager) view.findViewById(R.id.home_ViewPager);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();
        initData();
    }

    private void initView() {
        adapter = new HomeViewPagerAdapter(getFragmentManager());

    }

    private void initData() {

        tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);

        StringRequest request = new StringRequest(Contants.TAG_ITEM_URL, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                //   Log.i("lanou", response + "*****");
                Gson gson = new Gson();
                TagItemResult tagItemResult = gson.fromJson(response, TagItemResult.class);
                if (tagItemResult.getMsg().equals("ok")) {
                    List<TagItem> tagItems = tagItemResult.getData().getTag_items();
                    if (tagItems != null && tagItems.size() > 0) {
                        adapter.addData(tagItems);
                        viewPager.setAdapter(adapter);
                        tabLayout.setupWithViewPager(viewPager);

                    }
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
            }
        }){
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                // 添加请求头 在请求头中增加属性
                Map<String, String> map = new HashMap<String, String>();
                // 在请求头中添加apikey
                map.put("Accept-Encoding", "");
                return map;
            }

            @Override
            protected Response<String> parseNetworkResponse(
                    NetworkResponse response) {
                try {
                    String jsonString = new String(response.data, "UTF-8");
                    return Response.success(jsonString,
                            HttpHeaderParser.parseCacheHeaders(response));
                } catch (UnsupportedEncodingException e) {
                    return Response.error(new ParseError(e));
                } catch (Exception je) {
                    return Response.error(new ParseError(je));
                }
            }
        };
        VolleySingleton volleySingleton=VolleySingleton.getInstance();
       volleySingleton.addRequest(request);
    }

}
